package com.wibo.account;

import java.util.ArrayList;
import java.util.List;

import weibo4android.Count;
import weibo4android.Status;
import weibo4android.Weibo;
import weibo4android.WeiboException;

import com.wibo.common.FileDbHelper;
import com.wibo.common.HScrollViewGroup;
import com.wibo.common.Utility;
import com.wibo.common.WeiboItem;
import com.wibo.common.HScrollViewGroup.OnItemSelectedListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

public class WeiboListActivity extends Activity implements OnItemSelectedListener, OnClickListener, OnItemClickListener, OnScrollListener {
	
	public static final String TAG = "WeiboListActivity";
	private static Weibo mWeibo = null;
	private static OAuthConstant mAuthConstant = null;
	protected FileDbHelper mDbHelper = null;
	
	CUserManager mUserManager = null;
	private boolean mbRunning = false;
	
	private int mType = -1;
	
	public static final int HOT_REPOST = 0;
	public static final int HOT_COMMENTS = 1;
	public static final int HOT_TRENDS = 2;
	public static final int PUBLIC_WEIBO = 3;
	
	public static final int LIST_01 = 0;
	public static final int LIST_02 = 1;
	public static final int LIST_03 = 2;
	
	
	private HScrollViewGroup mListContentView;
	
	private FrameLayout mFrameLayout03;
	private RelativeLayout mBarLayout;
	private EmotionsParser mParser;
	
	private TextView mTextView01 = null;
	private TextView mTextView02 = null;
	private TextView mTextView03 = null;
	
	private ListView mList01;
	private ListView mList02;
	private ListView mList03;
	

	private ArrayList<WeiboItem> mListData01 = null;
	private ArrayList<WeiboItem> mListData02 = null;
	private ArrayList<WeiboListItemAdapter> mWeiboListAdapterList = null;
	

	private int mCurrentScreen = 0;
	
	private static final int GET_REPOSTS_FINISH = 0;
	private static final int GET_REPOSTS_IMG_FINISH = 1;
	private static final int GET_REPOSTS_ERROR = 2;
	private static final int GET_COMMENTS_FINISH = 3;
	private static final int GET_COMMENTS_IMG_FINISH = 4;
	private static final int GET_COMMENTS_ERROR = 5;
//	private static final int GET_REPOSTS_FINISH = 0;
//	private static final int GET_REPOSTS_IMG_FINISH = 1;
//	private static final int GET_REPOSTS_ERROR = 2;
	
	private Handler mHandler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			if(!mbRunning){

				Log.e(TAG, "stop by not running flag!");
	    		return;
	    	}
			switch (msg.what) {
			case GET_REPOSTS_FINISH:
				if (mCurrentScreen == 0){
					if (msg.obj != null) {
						mListData01.clear();
						mListData01.addAll((ArrayList<WeiboItem>) msg.obj);
						mWeiboListAdapterList.get(0).notifyDataSetChanged();
						
						loadWeiboPicList1(0, 5);
					}
				}else{
					if (msg.obj != null) {
						mListData02.clear();
						mListData02.addAll((ArrayList<WeiboItem>) msg.obj);
						mWeiboListAdapterList.get(1).notifyDataSetChanged();
						
						loadWeiboPicList2(0, 5);
					}
				}
				break;
			case GET_REPOSTS_IMG_FINISH:
				if (mCurrentScreen == 0){
					mWeiboListAdapterList.get(0).notifyDataSetChanged();
				}else{
					mWeiboListAdapterList.get(1).notifyDataSetChanged();
				}
				break;
			case GET_REPOSTS_ERROR:
				if (mCurrentScreen == 0){
					
				}else{
					
				}
				break;
			case GET_COMMENTS_FINISH:
				if (mCurrentScreen == 0){
					if (msg.obj != null) {
						mListData01.clear();
						mListData01.addAll((ArrayList<WeiboItem>) msg.obj);
						mWeiboListAdapterList.get(0).notifyDataSetChanged();
						
						loadWeiboPicList1(0, 5);
					}
				}else{
					if (msg.obj != null) {
						mListData02.clear();
						mListData02.addAll((ArrayList<WeiboItem>) msg.obj);
						mWeiboListAdapterList.get(1).notifyDataSetChanged();
						
						loadWeiboPicList2(0, 5);
					}
				}
				break;
			case GET_COMMENTS_IMG_FINISH:
				if (mCurrentScreen == 0){
					mWeiboListAdapterList.get(0).notifyDataSetChanged();
				}else{
					mWeiboListAdapterList.get(1).notifyDataSetChanged();
				}
				break;
			case GET_COMMENTS_ERROR:
				break;
			}
		}
	};
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}
	
	private void sendMessage(int what, Object obj) {
		Message msg = mHandler.obtainMessage(what, obj);
		mHandler.sendMessage(msg);
	}
	
	private void sendMessage(int what, int arg1, int arg2, Object obj) {
		Message msg = mHandler.obtainMessage(what, arg1, arg2, obj);
		mHandler.sendMessage(msg);
	}
	
	private void setContentViewSkin() {
		int skin = mUserManager.getMySkin();
		switch (skin) {
		case 0:
			setContentView(R.layout.weibolist);
			break;
		case 1:
			setContentView(R.layout.weibolist_2);
			break;
		default:
			setContentView(R.layout.weibolist);
			break;
		}
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//setContentView(R.layout.weibolist);
		
		mbRunning = true;
		mUserManager = CUserManager.getInstance(this); 
		setContentViewSkin();
		
		if (mAuthConstant == null)
			mAuthConstant = OAuthConstant.getInstance();

		if (mWeibo == null)
			mWeibo = mAuthConstant.getWeibo();

		if (mDbHelper == null)
			mDbHelper = new FileDbHelper(this);
		
		mListContentView = (HScrollViewGroup) findViewById(R.id.weiboListContent);
		mListContentView.setOnItemSelectedListener(this);
		
		mTextView01 = (TextView) findViewById(R.id.Text01);
		mTextView02 = (TextView) findViewById(R.id.Text02);
		mTextView03 = (TextView) findViewById(R.id.Text03);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mType = extras.getInt("weiboType");
			
			switch (mType) {
			case 0:
				setTitle(getString(R.string.hotRepost));
				initHeaderBar();
				mList01 = (ListView) findViewById(R.id.List01);
				mList01.setVisibility(ListView.VISIBLE);
				mList02 = (ListView) findViewById(R.id.List02);
				mList02.setVisibility(ListView.VISIBLE);
				mBarLayout = (RelativeLayout) findViewById(R.id.scrollbarTipLayout02);
				mBarLayout.setVisibility(RelativeLayout.VISIBLE);
				mTextView01.setText(getString(R.string.dailyhotRepost));
				mTextView02.setText(getString(R.string.weeklyhotRepost));
				mListData01 = new ArrayList<WeiboItem>();
				mListData02 = new ArrayList<WeiboItem>();
				mWeiboListAdapterList = new ArrayList<WeiboListItemAdapter>(2);
				WeiboListItemAdapter adapter = new WeiboListItemAdapter(this);
				adapter.setAdapterData(mListData01);
				mWeiboListAdapterList.add(adapter);
				
				adapter = new WeiboListItemAdapter(this);
				adapter.setAdapterData(mListData02);
				mWeiboListAdapterList.add(adapter);
				
				mList01.setAdapter(mWeiboListAdapterList.get(0));
				mList02.setAdapter(mWeiboListAdapterList.get(1));
				mList01.setOnItemClickListener(this);
				mList02.setOnItemClickListener(this);
				mList01.setOnScrollListener(this);
				mList02.setOnScrollListener(this);

				mCurrentScreen = 0;
				setUiShow();
				getHotRepost();
				break;
				
			case 1:
				setTitle(getString(R.string.hotComments));
				initHeaderBar();
				mList01 = (ListView) findViewById(R.id.List01);
				mList01.setVisibility(ListView.VISIBLE);
				mList02 = (ListView) findViewById(R.id.List02);
				mList02.setVisibility(ListView.VISIBLE);
				mBarLayout = (RelativeLayout) findViewById(R.id.scrollbarTipLayout02);
				mBarLayout.setVisibility(RelativeLayout.VISIBLE);
				mTextView01.setText(getString(R.string.dailyhotComments));
				mTextView02.setText(getString(R.string.weeklyhotComments));
				mListData01 = new ArrayList<WeiboItem>();
				mListData02 = new ArrayList<WeiboItem>();
				mWeiboListAdapterList = new ArrayList<WeiboListItemAdapter>(2);
				
				adapter = new WeiboListItemAdapter(this);
				adapter.setAdapterData(mListData01);
				mWeiboListAdapterList.add(adapter);
				
				adapter = new WeiboListItemAdapter(this);
				adapter.setAdapterData(mListData02);
				mWeiboListAdapterList.add(adapter);;
				mList01.setAdapter(mWeiboListAdapterList.get(0));
				mList02.setAdapter(mWeiboListAdapterList.get(1));
				mList01.setOnItemClickListener(this);
				mList02.setOnItemClickListener(this);
				mList01.setOnScrollListener(this);
				mList02.setOnScrollListener(this);
				
				mCurrentScreen = 0;
				setUiShow();
				getHotComments();
				break;
			case 2:
				setTitle(getString(R.string.hotTrends));
				initHeaderBar();
				mFrameLayout03 = (FrameLayout) findViewById(R.id.TitleHelpLayout03);
				mFrameLayout03.setVisibility(FrameLayout.VISIBLE);
				mBarLayout = (RelativeLayout) findViewById(R.id.scrollbarTipLayout03);
				mBarLayout.setVisibility(RelativeLayout.VISIBLE);
				mTextView01.setText(getString(R.string.hourhotTrends));
				mTextView02.setText(getString(R.string.dailyhotTrends));
				mTextView03.setText(getString(R.string.weeklyhotTrends));
				
				break;
			case 3:
				
				break;
			}
		}else{
			finish();
		}
	}
	
    private void initHeaderBar(){
    	  
        FrameLayout selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout01);
        selectLayout.setSelected(true);
    
        selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout02);
        selectLayout.setSelected(true);
     
        selectLayout = (FrameLayout)findViewById(R.id.TitleHelpLayout03);
        selectLayout.setSelected(true);

        Button btnAllTop = (Button) findViewById(R.id.Btn01);
        Button btnFreeTop = (Button) findViewById(R.id.Btn02);
        Button btnPayTop = (Button) findViewById(R.id.Btn03);
        
        btnAllTop.setOnClickListener(this);
        btnFreeTop.setOnClickListener(this);
        btnPayTop.setOnClickListener(this);
        
    }
	
	private void setUiShow(){
		switch (mType) {
		case HOT_REPOST:
		case HOT_COMMENTS:
			if (mCurrentScreen == 0){
				mTextView01.setSelected(true);
				mTextView02.setSelected(false);
			}else{
				mTextView01.setSelected(false);
				mTextView02.setSelected(true);
			}
			break;
		}

	}
	
	private Thread mGetHotRepostThread = null;
	private void getHotRepost(){
		if (mGetHotRepostThread != null) {
			if (mGetHotRepostThread.isAlive()) {
				mGetHotRepostThread.interrupt();
			}
			mGetHotRepostThread = null;
		}
		
		mGetHotRepostThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					List<Status> statuses = null;
					ArrayList<WeiboItem> listData = new ArrayList<WeiboItem>();
					if (mCurrentScreen == 0){
						if (mListData01 == null) {
							mListData01 = new ArrayList<WeiboItem>();
						}
						statuses = mWeibo.getHotRepostDailyline(50, 0);
					}else{
						if (mListData02 == null) {
							mListData02 = new ArrayList<WeiboItem>();
						}
						statuses = mWeibo.getHotRepostWeeklyline(50, 0);
					}
					
					if (statuses != null && statuses.size() > 0) {
						ArrayList<WeiboItem> itemDataList = new ArrayList<WeiboItem>();
						
						for (Status status : statuses) {
							WeiboItem itemData = new WeiboItem();
							itemData.setmUserName(status.getUser()
									.getName());
							itemData.setmSentTime(Utility.formatDate(status
									.getCreatedAt()));
							itemData.setmWeiboText(status.getText());
							itemData.setmClientName(status.getSource());

							itemData.setmWeiboUserPic(status.getUser()
									.getProfileImageURL().toString());
							if (status.getThumbnail_pic() != null && !status.getThumbnail_pic().isEmpty()){
								itemData.setmWeiboPic(status.getThumbnail_pic());
								itemData.setmWeiboMidPic(status.getBmiddle_pic());
								itemData.setmWeiboLargePic(status.getOriginal_pic());
							}

							itemData.setmUserId(status.getUser().getId());
							itemData.setmVLogo(status.getUser().isVerified());
							itemData.setmLatitude(String.valueOf(status.getLatitude()));
							itemData.setmLongitude(String.valueOf(status.getLongitude()));
							
							Status transmitStatus = status.getRetweeted_status();
							if (transmitStatus != null) {
								itemData.setmRtrootText(transmitStatus.getText());
								itemData.setmRtrootWeiboPic(transmitStatus.getThumbnail_pic());
								itemData.setmRtrootWeiboMidPic(transmitStatus.getBmiddle_pic());
								itemData.setmRtrootWeiboLargePic(transmitStatus.getOriginal_pic());
								itemData.setmRtrootWeiboId(transmitStatus.getId());
								itemData.setmRtrootUsername(transmitStatus.getUser().getName());
								itemData.setmRtrootUserId(transmitStatus.getUser().getId());
								itemData.setmRtrootVLogo(transmitStatus.getUser().isVerified());
							}
							itemData.setmWeiboId(status.getId());
							itemDataList.add(itemData);
						}
						
						if (itemDataList.size() > 0) {
							listData.addAll(itemDataList);
						}
						
						// 获取评论和转发数
						int leftSize = listData.size();
						List<Count> counts = null;
						int i = 0;
						int j = 0;
						int startIndex = 0;
						String ids = "";
						if (leftSize > WiboMainActivity.GET_COUNT_MAX) {
							do {
								for (; i < startIndex + WiboMainActivity.GET_COUNT_MAX; i++) {
									ids += String.valueOf(listData.get(i).getmWeiboId()) + ",";
								}
								ids = ids.substring(0, ids.length() - 1);

								counts = mWeibo.getCounts(ids);
								
								if (counts != null) {
									for (Count count : counts) {
										for (j = 0; j < listData.size(); j++) {
											if (listData.get(j).getmWeiboId() == count.getId()){
												listData.get(j).setmCommentCount(String.valueOf(count.getComments()));
												listData.get(j).setmTransmitCount(String.valueOf(count.getRt()));
												break;
											}
										}
									}
								}
								startIndex += WiboMainActivity.GET_COUNT_MAX;
							} while ((leftSize -= WiboMainActivity.GET_COUNT_MAX) > WiboMainActivity.GET_COUNT_MAX);
						}
						
						if (leftSize > 0 && leftSize <= WiboMainActivity.GET_COUNT_MAX) {
							ids = "";
							for (; i < listData.size(); i++) {
								ids += String.valueOf(listData.get(i).getmWeiboId()) + ",";
							}
							ids = ids.substring(0, ids.length() - 1);

							counts = mWeibo.getCounts(ids);
							
							if (counts != null) {
								for (Count count : counts) {									
									for (j = 0; j < listData.size(); j++) {
										if (listData.get(j).getmWeiboId() == count.getId()){
											listData.get(j).setmCommentCount(String.valueOf(count.getComments()));
											listData.get(j).setmTransmitCount(String.valueOf(count.getRt()));
											break;
										}
									}
								}
							}
						}
						
						if (itemDataList.size() > 0) {
							sendMessage(GET_REPOSTS_FINISH, mCurrentScreen, 0, listData);
						}else{
							sendMessage(GET_REPOSTS_FINISH, mCurrentScreen, 0, null);
						}
//						sendMessage(REFRESH_LIST, null);
					}
				} catch (WeiboException e) {
					sendMessage(GET_REPOSTS_ERROR, mCurrentScreen, 0, e.getMessage());
					e.printStackTrace();
				} catch (Exception e) {
					sendMessage(GET_REPOSTS_ERROR, mCurrentScreen, 0, e.getMessage());
					e.printStackTrace();
				}
			}
		});
		mGetHotRepostThread.start();
	}
	
	private Thread mGetHotCommentsThread = null;
	private void getHotComments(){
		if (mGetHotCommentsThread != null) {
			if (mGetHotCommentsThread.isAlive()) {
				mGetHotCommentsThread.interrupt();
			}
			mGetHotCommentsThread = null;
		}
		
		mGetHotCommentsThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					List<Status> statuses = null;
					ArrayList<WeiboItem> listData = new ArrayList<WeiboItem>();
					if (mCurrentScreen == 0){
						if (mListData01 == null) {
							mListData01 = new ArrayList<WeiboItem>();
						}
						statuses = mWeibo.getHotCommentsDailyline(50, 0);
					}else{
						if (mListData02 == null) {
							mListData02 = new ArrayList<WeiboItem>();
						}
						statuses = mWeibo.getHotCommentsWeeklyline(50, 0);
					}
					
					if (statuses != null && statuses.size() > 0) {
						ArrayList<WeiboItem> itemDataList = new ArrayList<WeiboItem>();
						
						for (Status status : statuses) {
							WeiboItem itemData = new WeiboItem();
							itemData.setmUserName(status.getUser()
									.getName());
							itemData.setmSentTime(Utility.formatDate(status
									.getCreatedAt()));
							itemData.setmWeiboText(status.getText());
							itemData.setmClientName(status.getSource());

							itemData.setmWeiboUserPic(status.getUser()
									.getProfileImageURL().toString());
							if (status.getThumbnail_pic() != null && !status.getThumbnail_pic().isEmpty()){
								itemData.setmWeiboPic(status.getThumbnail_pic());
								itemData.setmWeiboMidPic(status.getBmiddle_pic());
								itemData.setmWeiboLargePic(status.getOriginal_pic());
							}

							itemData.setmUserId(status.getUser().getId());
							itemData.setmVLogo(status.getUser().isVerified());
							itemData.setmLatitude(String.valueOf(status.getLatitude()));
							itemData.setmLongitude(String.valueOf(status.getLongitude()));
							
							Status transmitStatus = status.getRetweeted_status();
							if (transmitStatus != null) {
								itemData.setmRtrootText(transmitStatus.getText());
								itemData.setmRtrootWeiboPic(transmitStatus.getThumbnail_pic());
								itemData.setmRtrootWeiboMidPic(transmitStatus.getBmiddle_pic());
								itemData.setmRtrootWeiboLargePic(transmitStatus.getOriginal_pic());
								itemData.setmRtrootWeiboId(transmitStatus.getId());
								itemData.setmRtrootUsername(transmitStatus.getUser().getName());
								itemData.setmRtrootUserId(transmitStatus.getUser().getId());
								itemData.setmRtrootVLogo(transmitStatus.getUser().isVerified());
							}
							itemData.setmWeiboId(status.getId());
							itemDataList.add(itemData);
						}
						
						if (itemDataList.size() > 0) {
							listData.addAll(itemDataList);
						}
						
						// 获取评论和转发数
						int leftSize = listData.size();
						List<Count> counts = null;
						int i = 0;
						int j = 0;
						int startIndex = 0;
						String ids = "";
						if (leftSize > WiboMainActivity.GET_COUNT_MAX) {
							do {
								for (; i < startIndex + WiboMainActivity.GET_COUNT_MAX; i++) {
									ids += String.valueOf(listData.get(i).getmWeiboId()) + ",";
								}
								ids = ids.substring(0, ids.length() - 1);

								counts = mWeibo.getCounts(ids);
								
								if (counts != null) {
									for (Count count : counts) {
										for (j = 0; j < listData.size(); j++) {
											if (listData.get(j).getmWeiboId() == count.getId()){
												listData.get(j).setmCommentCount(String.valueOf(count.getComments()));
												listData.get(j).setmTransmitCount(String.valueOf(count.getRt()));
												break;
											}
										}
									}
								}
								startIndex += WiboMainActivity.GET_COUNT_MAX;
							} while ((leftSize -= WiboMainActivity.GET_COUNT_MAX) > WiboMainActivity.GET_COUNT_MAX);
						}
						
						if (leftSize > 0 && leftSize <= WiboMainActivity.GET_COUNT_MAX) {
							ids = "";
							for (; i < listData.size(); i++) {
								ids += String.valueOf(listData.get(i).getmWeiboId()) + ",";
							}
							ids = ids.substring(0, ids.length() - 1);

							counts = mWeibo.getCounts(ids);
							
							if (counts != null) {
								for (Count count : counts) {									
									for (j = 0; j < listData.size(); j++) {
										if (listData.get(j).getmWeiboId() == count.getId()){
											listData.get(j).setmCommentCount(String.valueOf(count.getComments()));
											listData.get(j).setmTransmitCount(String.valueOf(count.getRt()));
											break;
										}
									}
								}
							}
						}
						
						if (itemDataList.size() > 0) {
							sendMessage(GET_COMMENTS_FINISH, mCurrentScreen, 0, listData);
						}else{
							sendMessage(GET_COMMENTS_FINISH, mCurrentScreen, 0, null);
						}
//						sendMessage(REFRESH_LIST, null);
					}
				} catch (WeiboException e) {
					sendMessage(GET_COMMENTS_ERROR, mCurrentScreen, 0, e.getMessage());
					e.printStackTrace();
				}
			}
		});
		mGetHotCommentsThread.start();
	}


	/* (non-Javadoc)
	 * @see android.app.Activity#onDestroy()
	 */
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mGetHotRepostThread != null) {
			if (mGetHotRepostThread.isAlive()) {
				mGetHotRepostThread.interrupt();
			}
			mGetHotRepostThread = null;
		}
		
		if (mGetHotCommentsThread != null) {
			if (mGetHotCommentsThread.isAlive()) {
				mGetHotCommentsThread.interrupt();
			}
			mGetHotCommentsThread = null;
		}
		
		if (mGetPicThreadList1 != null) {
			if (mGetPicThreadList1.isAlive()) {
				mGetPicThreadList1.interrupt();
			}
			mGetPicThreadList1 = null;
		}
		
		if (mGetPicThreadList2 != null) {
			if (mGetPicThreadList2.isAlive()) {
				mGetPicThreadList2.interrupt();
			}
			mGetPicThreadList2 = null;
		}
	}

	@Override
	public void onSelected(int selectedIndex) {
		switch (mType) {
		case HOT_REPOST:
		case HOT_COMMENTS:
			switch (selectedIndex) {
			case 0:
				if (LIST_01 != mCurrentScreen){
					if (LIST_02 == mCurrentScreen){
						//创建Animation对象  
						int animId = 0;
						if (mType == HOT_REPOST || mType == HOT_COMMENTS)
							animId = R.anim.total_2_21;
						else if (mType == HOT_TRENDS)
							animId = R.anim.apphead21;
						
						Animation animation = AnimationUtils.loadAnimation(  
						getApplicationContext(), animId);  
						animation.setFillAfter(true);
						//组件播放动画  
						mBarLayout.startAnimation(animation); 
					}else{
						Animation animation = AnimationUtils.loadAnimation(  
						getApplicationContext(), R.anim.apphead31);  
						animation.setFillAfter(true); 
						mBarLayout.startAnimation(animation);
					}
					
					SetTypeLayoutVisible(LIST_01);
					setUiShow();
					
					if (mType == HOT_REPOST){
						if (mListData01.size() == 0) {
							getHotRepost();
						}
					} else if (mType == HOT_COMMENTS) {
						if (mListData01.size() == 0) {
							getHotComments();
						}
					} else if (mType == HOT_TRENDS) {
						
					}
				}
				break;
			case 1:
				if (LIST_02 != mCurrentScreen){
					if (LIST_01 == mCurrentScreen){
						//创建Animation对象  
						int animId = 0;
						if (mType == HOT_REPOST || mType == HOT_COMMENTS)
							animId = R.anim.total_2_12;
						else if (mType == HOT_TRENDS)
							animId = R.anim.apphead12;
						
						Animation animation = AnimationUtils.loadAnimation(  
						getApplicationContext(), animId);  
						animation.setFillAfter(true);
						//组件播放动画  
						mBarLayout.startAnimation(animation); 
					}else{
						Animation animation = AnimationUtils.loadAnimation(  
						getApplicationContext(), R.anim.apphead13);  
						animation.setFillAfter(true); 
						mBarLayout.startAnimation(animation);
					}
					
					SetTypeLayoutVisible(LIST_02);
					setUiShow();
					
					if (mType == HOT_REPOST){
						if (mListData02.size() == 0) {
							getHotRepost();
						}
					} else if (mType == HOT_COMMENTS) {
						if (mListData02.size() == 0) {
							getHotComments();
						}
					} else if (mType == HOT_TRENDS) {
						
					}
				}
				break;
			}
			break;
		case HOT_TRENDS:
			break;
		}
	}
	
	// 设置哪个列表头需要显示,同时清空临时数据
    private void SetTypeLayoutVisible(int type){
    	switch (mType){
    	case HOT_REPOST:
    		mCurrentScreen = type;
    		
    		switch(type){
        	case LIST_01:
        		mListContentView.showScreen(0);
        		mList01.setVisibility(View.VISIBLE);
        		mList02.setVisibility(View.INVISIBLE);

    			break;
        	case LIST_02:
        		mListContentView.showScreen(1);
        		mList01.setVisibility(View.INVISIBLE);
        		mList02.setVisibility(View.VISIBLE);
        		
        		break;
    		}
    		break;
    	case HOT_COMMENTS:
    		mCurrentScreen = type;
    		
    		switch(type){
        	case LIST_01:
        		mListContentView.showScreen(0);
        		mList01.setVisibility(View.VISIBLE);
        		mList02.setVisibility(View.INVISIBLE);

    			break;
        	case LIST_02:
        		mListContentView.showScreen(1);
        		mList01.setVisibility(View.INVISIBLE);
        		mList02.setVisibility(View.VISIBLE);
        		
        		break;
    		}
    		break;
    	case HOT_TRENDS:
    		break;
    	}

    }

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.Btn01:
			
			if (LIST_01 != mCurrentScreen){
				if (LIST_02 == mCurrentScreen){
					//创建Animation对象  
					int animId = 0;
					if (mType == HOT_REPOST || mType == HOT_COMMENTS)
						animId = R.anim.total_2_21;
					else if (mType == HOT_TRENDS)
						animId = R.anim.apphead21;
					
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), animId);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.apphead31);  
					animation.setFillAfter(true); 
					mBarLayout.startAnimation(animation);
				}
				
				SetTypeLayoutVisible(LIST_01);
				setUiShow();
				
				if (mType == HOT_REPOST){
					if (mListData01.size() == 0) {
						getHotRepost();
					}
				} else if (mType == HOT_COMMENTS) {
					if (mListData01.size() == 0) {
						getHotComments();
					}
				} else if (mType == HOT_TRENDS) {
					
				}
			}
			
			break;
		case R.id.Btn02:
			
			if (LIST_02 != mCurrentScreen){
				if (LIST_01 == mCurrentScreen){
					//创建Animation对象  
					int animId = 0;
					if (mType == HOT_REPOST || mType == HOT_COMMENTS)
						animId = R.anim.total_2_12;
					else if (mType == HOT_TRENDS)
						animId = R.anim.apphead12;
					
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), animId);  
					animation.setFillAfter(true);
					//组件播放动画  
					mBarLayout.startAnimation(animation); 
				}else{
					Animation animation = AnimationUtils.loadAnimation(  
					getApplicationContext(), R.anim.apphead13);  
					animation.setFillAfter(true); 
					mBarLayout.startAnimation(animation);
				}
				
				SetTypeLayoutVisible(LIST_02);
				setUiShow();
				
				if (mType == HOT_REPOST){
					if (mListData02.size() == 0) {
						getHotRepost();
					}
				} else if (mType == HOT_COMMENTS) {
					if (mListData02.size() == 0) {
						getHotComments();
					}
				} else if (mType == HOT_TRENDS) {
					
				}
			}
			
			break;
		case R.id.Btn03:
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		switch (mCurrentScreen) {
		case 0:
			Intent intent = new Intent(WeiboListActivity.this, WeiboDetailsActivity.class);
    		Bundle bundle = new Bundle();
    		bundle.putLong("weiboId", mListData01.get(arg2).getmWeiboId());
    		bundle.putSerializable("weiboItem", mListData01.get(arg2));
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case 1:
			intent = new Intent(WeiboListActivity.this, WeiboDetailsActivity.class);
    		bundle = new Bundle();
    		bundle.putLong("weiboId", mListData02.get(arg2).getmWeiboId());
    		bundle.putSerializable("weiboItem", mListData02.get(arg2));
			intent.putExtras(bundle);
			startActivity(intent);
			break;
		case 2:
			break;
		}
	}
	
	private Thread mGetPicThreadList1 = null;
	
	private void loadWeiboPicList1(final int startIndex, final int lastIndex) {
		
		if (mGetPicThreadList1 != null) {
			if (mGetPicThreadList1.isAlive()) {
				mGetPicThreadList1.interrupt();
			}
			mGetPicThreadList1 = null;
		}
		
		mGetPicThreadList1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
				int size = mListData01.size();
				String path = null;
				for (int i = startIndex; lastIndex < size && i <= lastIndex; i++) {
					path = Utility.getImagePath(WeiboListActivity.this, mListData01.get(i).getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC);
					if (path != null) {
						mListData01.get(i).setmIsLoadHeadPic(true);
					}
										
					if (mListData01.get(i).getmWeiboPic() != null && !mListData01.get(i).getmWeiboPic().isEmpty()){
						path = Utility.getImagePath(WeiboListActivity.this, mListData01.get(i).getmWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC);
						if (path != null) {
							mListData01.get(i).setmIsLoadWeiboPic(true);
						}
					}
					
					if (mListData01.get(i).getmRtrootText() != null && !mListData01.get(i).getmRtrootText().isEmpty()) {
						if (mListData01.get(i).getmRtrootWeiboPic() != null && !mListData01.get(i).getmRtrootWeiboPic().isEmpty()){
							path = Utility.getImagePath(WeiboListActivity.this, mListData01.get(i).getmRtrootWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC);
							if (path != null) {
								mListData01.get(i).setmIsLoadRootPic(true);
							}
						}
					}
					
					
					sendMessage(GET_COMMENTS_IMG_FINISH, null);
				}
			}
		});
		
		mGetPicThreadList1.start();
		
	}
	
private Thread mGetPicThreadList2 = null;
	
	private void loadWeiboPicList2(final int startIndex, final int lastIndex) {
		
		if (mGetPicThreadList2 != null) {
			if (mGetPicThreadList2.isAlive()) {
				mGetPicThreadList2.interrupt();
			}
			mGetPicThreadList2 = null;
		}
		
		mGetPicThreadList2 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				
				Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
				int size = mListData02.size();
				String path = null;
				for (int i = startIndex; lastIndex < size && i <= lastIndex; i++) {
					path = Utility.getImagePath(WeiboListActivity.this, mListData02.get(i).getmWeiboUserPic(), Utility.WEIBO_PIC, Utility.USER_PIC);
					if (path != null) {
						mListData02.get(i).setmIsLoadHeadPic(true);
					}
										
					if (mListData02.get(i).getmWeiboPic() != null && !mListData02.get(i).getmWeiboPic().isEmpty()){
						path = Utility.getImagePath(WeiboListActivity.this, mListData02.get(i).getmWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC);
						if (path != null) {
							mListData02.get(i).setmIsLoadWeiboPic(true);
						}
					}
					
					if (mListData02.get(i).getmRtrootText() != null && !mListData02.get(i).getmRtrootText().isEmpty()) {
						if (mListData02.get(i).getmRtrootWeiboPic() != null && !mListData02.get(i).getmRtrootWeiboPic().isEmpty()){
							path = Utility.getImagePath(WeiboListActivity.this, mListData02.get(i).getmRtrootWeiboPic(), Utility.WEIBO_PIC, Utility.THUMBNAIL_PIC);
							if (path != null) {
								mListData02.get(i).setmIsLoadRootPic(true);
							}
						}
					}
					
					
					sendMessage(GET_REPOSTS_IMG_FINISH, null);
				}
			}
		});
		
		mGetPicThreadList2.start();
		
	}
	
	private int mLastFreshImageIndexList1 = 0;
	private int mLastFreshImageIndexList2 = 0;
	
	private void refreshImageForList1(int viewId){
		if(mListData01 != null && mListData01.size() > 0){
			int startIndex = mList01.getFirstVisiblePosition();
			int endIndex = mList01.getLastVisiblePosition();
			
			// 设置上一次的刷新位置
			mLastFreshImageIndexList1 = startIndex;
			
			if(startIndex > 0){
				--startIndex;
			}
			if(endIndex >= mListData01.size()){
				endIndex = mListData01.size() - 1;
			}
			
			for (int i = startIndex; i <= endIndex; i++) {
				if (!mListData01.get(i).ismIsLoadHeadPic() || 
						(mListData01.get(i).getmWeiboPic() != null && mListData01.get(i).ismIsLoadWeiboPic()) ||
						(mListData01.get(i).getmRtrootWeiboPic() != null && mListData01.get(i).ismIsLoadRootPic())){
					loadWeiboPicList1(i, endIndex);
					return;
				}
			}
		}
	}
	
	private void refreshImageForList2(int viewId){
		if(mListData02 != null && mListData02.size() > 0){
			int startIndex = mList02.getFirstVisiblePosition();
			int endIndex = mList02.getLastVisiblePosition();
			
			// 设置上一次的刷新位置
			mLastFreshImageIndexList2 = startIndex;
			
			if(startIndex > 0){
				--startIndex;
			}
			if(endIndex >= mListData02.size()){
				endIndex = mListData02.size() - 1;
			}
			
			for (int i = startIndex; i <= endIndex; i++) {
				if (!mListData02.get(i).ismIsLoadHeadPic() || 
						(mListData02.get(i).getmWeiboPic() != null && mListData02.get(i).ismIsLoadWeiboPic()) ||
						(mListData02.get(i).getmRtrootWeiboPic() != null && mListData02.get(i).ismIsLoadRootPic())){
					loadWeiboPicList2(i, endIndex);
					return;
				}
			}
		}
	}
	
	// 记录上次获取图片信息的时间
	private int mLastFreshImageIndex = 0;
	public static final int DOWNLOAD_IMAGE_MAX_SIZE = 5;

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		switch (view.getId()) {
		case R.id.List01:
			if(Math.abs(firstVisibleItem - mLastFreshImageIndexList1) >= DOWNLOAD_IMAGE_MAX_SIZE){
				refreshImageForList1(view.getId());
			}
			break;
		case R.id.List02:
			if(Math.abs(firstVisibleItem - mLastFreshImageIndexList2) >= DOWNLOAD_IMAGE_MAX_SIZE){
				refreshImageForList2(view.getId());
			}
			break;
		}
		
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if(scrollState == SCROLL_STATE_IDLE){
			switch (view.getId()) {
			case R.id.List01:
				refreshImageForList1(view.getId());
				
				break;
			case R.id.List02:
				refreshImageForList2(view.getId());
				
				break;
			}
		}
	}

}
